package com.guaoran.other.singleton.lazy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author : 孤傲然
 * @Description :
 *  懒汉模式:静态内部类
 * @date :2018/5/13 13:45
 */
public class LazyInnerSingleton {
    /**构造方法私有**/
    private LazyInnerSingleton(){
        if(LazyInner.singleton != null){
            throw new RuntimeException("不允许使用反射");
        }
    }
    private static class LazyInner{
        private static final LazyInnerSingleton singleton = new LazyInnerSingleton();
    }
    public static LazyInnerSingleton getInstance(){
        return LazyInner.singleton;
    }


    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Constructor<LazyInnerSingleton> constructor = LazyInnerSingleton.class.getDeclaredConstructor(null);
//        constructor.setAccessible(true);
        LazyInnerSingleton lazyInnerSingleton = constructor.newInstance();
        LazyInnerSingleton lazyInnerSingleton1 = LazyInnerSingleton.getInstance();
        System.out.println(lazyInnerSingleton == lazyInnerSingleton1);
    }
}
